package adminServlet;

import java.io.IOException;

import java.sql.Timestamp;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import routeEntity.Route;
import routeImplment.RouteImplment;

/**
 * Servlet implementation class AddRoute
 */
public class AddRoute extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddRoute() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String Rname = request.getParameter("Rname");
        String fromStation = request.getParameter("fromStation");
        String toStation = request.getParameter("toStation");
        String fromTime = request.getParameter("fromTime");
        String toTime = request.getParameter("toTime");
        String seatNumber = request.getParameter("seatNumber");
        String seatPrice = request.getParameter("seatPrice");
        //将表单获得的路线信息封装为一个Route对象
        String fromTime1 = fromTime.substring(0, 10) + ' ' + fromTime.substring(11, 16) + ":00.0";
        Timestamp f = Timestamp.valueOf(fromTime1);
        String toTime1 = toTime.substring(0, 10) + ' ' + toTime.substring(11, 16) + ":00.0";
        Timestamp t = Timestamp.valueOf(fromTime1);

        Route route = new Route();
        route.setRname(Rname);
        route.setFromStation(fromStation);
        route.setToStation(toStation);
        route.setFromTime(f);
        route.setToTime(t);
        route.setSeatNumber(Integer.parseInt(seatNumber));
        route.setSeatPrice(Integer.parseInt(seatPrice));

        HttpSession session = request.getSession();

        RouteImplment routeImplment = new RouteImplment();
        if (routeImplment.AddRoute(route)) {
            session.setAttribute("routeList", routeImplment.GetAllRoute());
            request.getRequestDispatcher("adminRoute.jsp").forward(request, response);
        } else {
            System.out.print("添加失败");
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
